package org.performancetracker.servlet25;

import org.performancetracker.api.Aggregator;
import org.performancetracker.api.Tracker;
import org.performancetracker.AggregatorFactory;
import org.performancetracker.TrackerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;


/**
 * This filter tracks the performance of the request from entry to exit.
 *
 * @author: jaronschut
 */

public class HttpRequestMonitoringFilter implements Filter {
    private static final String LOG_INIT_1 = ": Initializing HttpRequestMonitoringFilter, aggregatorName set to '";
    private static final String LOG_INIT_2 = "'";

    private static final String CONFIG_AGGRAGATOR_NAME = "aggregatorName";
    private static final String DEFAULT_AGGREGATOR_NAME = "Tracker.Web.Request";

    private static String AGGREGATOR_NAME = DEFAULT_AGGREGATOR_NAME;
    private static Aggregator TRACKER_AGGREGATOR;

    private FilterConfig filterConfig;
         
    /**
     * Method of Filter interface, perform the performance tracking.
     *
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     */
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        if (servletRequest instanceof HttpServletRequest) {

            Tracker tracker = TrackerFactory.startTracker(TRACKER_AGGREGATOR);

            try {
                filterChain.doFilter(servletRequest, servletResponse);
            } catch (IOException io) {
                tracker.stop().aggregate(((HttpServletRequest) servletRequest).getRequestURI().toString() + io.getMessage());
                throw io;
            } catch (ServletException se) {
                tracker.stop().aggregate(((HttpServletRequest) servletRequest).getRequestURI().toString() + se.getMessage());
                throw se;
            }
            tracker.stop().aggregate(((HttpServletRequest) servletRequest).getRequestURI().toString());

        } else {
            filterChain.doFilter(servletRequest, servletResponse);
        }
    }

    /**
     * Start of filter live
     *
     * @param filterConfig
     * @throws ServletException
     */
    public void init(FilterConfig filterConfig) throws ServletException {
        Tracker tracker = TrackerFactory.startTracker();

        this.filterConfig = filterConfig;

        if (filterConfig.getInitParameter(CONFIG_AGGRAGATOR_NAME) != null) {
            AGGREGATOR_NAME = filterConfig.getInitParameter(CONFIG_AGGRAGATOR_NAME);
        }

        TRACKER_AGGREGATOR = AggregatorFactory.createTrackerAggregator(AGGREGATOR_NAME);

        tracker.stop().aggregate(HttpRequestMonitoringFilter.class.getSimpleName()
                + LOG_INIT_1
                + AGGREGATOR_NAME
                + LOG_INIT_2);
    }

    /**
     * End of Filter life.
     *
     */
    public void destroy() {

    }
}
